package com.banciyuan.bcywebview.biz.goods.shoppingcart;


import android.content.Context;
import android.text.TextUtils;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.DetailGoods;
import de.greenrobot.daoexample.model.Order;

public class ShoppingCartHelper {

    private static Map<String, DetailGoods> getShoppingMap(Context context) {
        Gson gson = new Gson();
        String buyCar = SPHelper.getString(context, SPConstant.SPNAME_BUYCAR, UserDataHelper.getInstance(context).getUid(), "{}");
        Map<String, DetailGoods> goods = gson.fromJson(buyCar, new TypeToken<LinkedHashMap<String, DetailGoods>>() {
        }.getType());
        return goods;
    }

    private static boolean isLogin(Context context) {
        if (TextUtils.isEmpty(UserDataHelper.getInstance(context).getUid())) {
            MyToast.show(context, context.getString(R.string.login_first));
            return false;
        } else {
            return true;
        }
    }

    public static boolean putShoppingItem(Context context, DetailGoods detailGoods) {
        if (isLogin(context)) {
            Map<String, DetailGoods> goods = getShoppingMap(context);
            String key = detailGoods.getZg_id() + "_" + detailGoods.getSelect_sku().getZps_id();
            if (!goods.containsKey(key)) {
                if (goods.size() < 10)
                    goods.put(key, detailGoods);
                else
                    MyToast.show(context, context.getString(R.string.shopping_cart_oversize));
            } else {
                DetailGoods removedGoods = goods.remove(key);
                removedGoods.setCount(removedGoods.getCount() + 1);
                goods.put(key, removedGoods);
            }
            SPHelper.putString(context, SPConstant.SPNAME_BUYCAR, UserDataHelper.getInstance(context).getUid(), new Gson().toJson(goods));
            return true;
        } else {
            return false;
        }
    }

    public static boolean deleteShoppingItemCount(Context context, DetailGoods detailGoods) {
        if (isLogin(context)) {
            Map<String, DetailGoods> goods = getShoppingMap(context);
            String key = detailGoods.getZg_id() + "_" + detailGoods.getSelect_sku().getZps_id();
            if (goods.containsKey(key)) {
                DetailGoods removedGoods = goods.get(key);
                if (removedGoods.getCount() > 1) {
                    removedGoods.setCount(removedGoods.getCount() - 1);
                    goods.put(key, removedGoods);
                }
            }
            SPHelper.putString(context, SPConstant.SPNAME_BUYCAR, UserDataHelper.getInstance(context).getUid(), new Gson().toJson(goods));
            return true;
        } else {
            return false;
        }
    }

    public static boolean putMutilShoppingItem(Context context, DetailGoods detailGoods, int count) {
        if (isLogin(context)) {
            Map<String, DetailGoods> goods = getShoppingMap(context);
            String key = detailGoods.getZg_id() + "_" + detailGoods.getSelect_sku().getZps_id();
            if (!goods.containsKey(key)) {
                if (goods.size() < 10)
                    goods.put(key, detailGoods);
                else
                    MyToast.show(context, context.getString(R.string.shopping_cart_oversize));
            } else {
                DetailGoods removedGoods = goods.remove(key);
                removedGoods.setCount(removedGoods.getCount() + count);
                goods.put(key, removedGoods);
            }
            SPHelper.putString(context, SPConstant.SPNAME_BUYCAR, UserDataHelper.getInstance(context).getUid(), new Gson().toJson(goods));
            return true;
        } else {
            return false;
        }
    }

    public static boolean deleteShoppingItem(Context context, DetailGoods detailGoods) {
        if (isLogin(context)) {
            Map<String, DetailGoods> goods = getShoppingMap(context);
            goods.remove(detailGoods.getZg_id() + "_" + detailGoods.getSelect_sku().getZps_id());
            SPHelper.putString(context, SPConstant.SPNAME_BUYCAR, UserDataHelper.getInstance(context).getUid(), new Gson().toJson(goods));
            return true;
        } else {
            return false;
        }
    }

    public static boolean deleteMutilSimpleGoodsItem(Context context, List<DetailGoods> dlist) {
        if (isLogin(context)) {
            Map<String, DetailGoods> goods = getShoppingMap(context);
            for (DetailGoods d : dlist) {
                goods.remove(d.getZg_id() + "_" + d.getZps_id());
            }
            SPHelper.putString(context, SPConstant.SPNAME_BUYCAR, UserDataHelper.getInstance(context).getUid(), new Gson().toJson(goods));
            return true;
        } else {
            return false;
        }
    }

    public static boolean deleteMutilOrderItem(Context context, List<Order> olist) {
        if (isLogin(context)) {
            Map<String, DetailGoods> goods = getShoppingMap(context);
            for (Order o : olist) {
                goods.remove(o.getZg_id() + "_" + o.getZps_id());
            }
            SPHelper.putString(context, SPConstant.SPNAME_BUYCAR, UserDataHelper.getInstance(context).getUid(), new Gson().toJson(goods));
            return true;
        } else {
            return false;
        }
    }

    public static List<DetailGoods> getShoppingItems(Context context) {
        Map<String, DetailGoods> goods = getShoppingMap(context);
        List<DetailGoods> list = new ArrayList<>(goods.values());
        Collections.reverse(list);
        return list;
    }

    public static DetailGoods getSingleShoppingItems(String key, Context context) {
        Map<String, DetailGoods> goods = getShoppingMap(context);
        return goods.get(key);
    }
}
